var Prod = function() {

	this.init = function() {
		
		$("body").delegate("[name='editProd']", "click", function(){
			
			var trNode = $(this).parents("tr");
			currentEditLink = $(this);
			var trNode = $(this).parents("tr");
			var pk_marbasclass = $(trNode.find("[name='pkProdCl']")[0]).val();
			var pk_org = $("#vendorOrg").val();
			var pk_group = $("#vendorGroup").val();
			if( pk_org==null||pk_org==""){
				showCfmMsg(getLanMsg("0ec22180-100163"), getLanMsg("0ec22180-100165"));
				return;
			}
			
			var myts = new Date();
			var formStr = $(this).parents("tr").find("#prod_filter").serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context + "/register/qryEcProdByPkProdcl?pk_marbasclass="+pk_marbasclass+"&myts=" + myts + "&pk_org="+pk_org +"&pk_group="+pk_group,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
					if (result != null && result != "[]") {
						var arr = eval(result);
						$(trNode.find("[name='prod_tree_list']")[0]).empty();
						for(var loop=0; loop<arr.length; loop++){
							var obj = arr[loop];
							var cate = $("<div></div>").addClass("prod_tree_category");
							var inputCheckbox = $("<input></input>").attr("type", "checkbox").val(obj.pk_prod_v);
							var blankLabel = $("<label></label>").css("width", "2px");
							var codeLabel = $("<label></label>").text(obj.prod_code).css({width:"85px"});
							var blankLabel1 = $("<label></label>").css("width", "5px");
							var labelProd = $("<label></label>").text(obj.prod_name).attr("title", "prodname").css({width:"85px"});
							var blankLabel2 = $("<label></label>").attr("width", "10px");
							var labelModule = $("<label></label>").text(obj.prod_module).attr("title","prodmodule").css({width:"85px"});
							var blankLabel3 = $("<label></label>").css("width", "10px");
							var labelType = $("<label></label>").text(obj.prod_type).attr("title","prodtype").css({width:"85px"});
							var labelLabel4 = blankLabel.clone();
							var dproducedate = $("<input></input>").attr("name","dproducedate").attr("type","text").attr("class","datepicker").attr("value","录入时间").css({width:"85px"});
								cate.append(inputCheckbox).append(codeLabel).append(labelProd)
								.append(labelModule).append(labelType).append(dproducedate);
							$(trNode.find("[name='prod_tree_list']")[0]).append(cate);
						}
					}else{
						$(trNode.find("[name='prod_tree_list']")[0]).empty();
					}
					$(trNode.find("[name='prodPicker']")[0]).slideDown("normal");
				}
			});
		});
		
		$("body").delegate("[name='filterProd']", "click", function(){
			
			var trNode = $(this).parents("tr");
			currentEditLink = $(this);
			var trNode = $(this).parents("tr");
			var pk_marbasclass = $(trNode.find("[name='pkProdCl']")[0]).val();
			var pk_org = $("#vendorOrg").val();
			var pk_group = $("#vendorGroup").val();
			if( pk_org==null||pk_org==""){
				showCfmMsg(getLanMsg("0ec22180-100163"), getLanMsg("0ec22180-100165"));
				return;
			}
			
			var myts = new Date();
			var formStr = $(this).parents("tr").find("#prod_filter").serialize();
			jQuery.ajax( {
				type : "POST",
				async : false,
				url : ebvp_context + "/register/qryEcProdByPkProdcl?pk_marbasclass="+pk_marbasclass+"&myts=" + myts + "&pk_org="+pk_org +"&pk_group="+pk_group,
				dataType : "text",
				data : "&" + formStr,
				success : function(result) {
					if (result != null && result != "[]") {
						var arr = eval(result);
						$(trNode.find("[name='prod_tree_list']")[0]).empty();
						for(var loop=0; loop<arr.length; loop++){
							var obj = arr[loop];
							var cate = $("<div></div>").addClass("prod_tree_category");
							var inputCheckbox = $("<input></input>").attr("type", "checkbox").val(obj.pk_prod_v);
							var blankLabel = $("<label></label>").attr("width", "10px");
							var codeLabel = $("<label></label>").text(obj.prod_code);
							var blankLabel1 = $("<label></label>").attr("width", "10px");
							var labelProd = $("<label></label>").text(obj.prod_name).attr("title", "prodname");
							var blankLabel2 = $("<label></label>").attr("width", "10px");
							var labelModule = $("<label></label>").text(obj.prod_module).attr("title", "prodmodule");
							var blankLabel3 = $("<label></label>").attr("width", "10px");
							var labelType = $("<label></label>").text(obj.prod_type).attr("title", "prodtype");
							
							cate.append(inputCheckbox).append(blankLabel).append(codeLabel).append(blankLabel1).append(labelProd)
								.append(blankLabel2).append(labelModule).append(blankLabel3).append(labelType);
							
							$(trNode.find("[name='prod_tree_list']")[0]).append(cate);
						}
					}else{
						$(trNode.find("[name='prod_tree_list']")[0]).empty();
					}
					$(trNode.find("[name='prodPicker']")[0]).slideDown("normal");
				}
			});
		});
		
		//确定
		$("body").delegate("[name='prodPicked']", "click", function(){
			var items = $(this).parents("[name='prodPicker']").find(":checked");
			
			if (items != null && items.length > 0) {
				var prodnames = "";
				var pkProds = "";
				var dproducedates = "";
				for (var loop = 0; loop < items.length; loop++) {
					var tempname = $(items[loop]).parent().find("[title='prodname']").text();
					var tempmodule = $(items[loop]).parent().find("[title='prodmodule']").text();
					var temptype = $(items[loop]).parent().find("[title='prodtype']").text();
					var tempdproducedate = $(items[loop]).parent().find("[name='dproducedate']").val();
					if (tempdproducedate == "录入时间" || tempdproducedate == "" || tempdproducedate == undefined) {
						tempdproducedate = null;
					}
					prodnames += tempname +"("+tempmodule+","+temptype+","+tempdproducedate+")"+", ";
					
					if(tempdproducedate == null){
						tempdproducedate = "-";   //占位，以防两个逗号之间为空，split出现的数组长度跟pk数组长度不匹配。
					}
					dproducedates += tempdproducedate + ", ";
					var temppk = $(items[loop]).val();
					pkProds +=temppk + ", ";
				}
				$(this).parents("tr").find("[name='prodName']").val(prodnames.substring(0, prodnames.lastIndexOf(",")));
				$(this).parents("tr").find("[name='pkProdv']").val(pkProds.substring(0, pkProds.lastIndexOf(",")));
				$(this).parents("tr").find("[name='dproducedates']").val(dproducedates.substring(0, dproducedates.lastIndexOf(",")));
				
			}
			$(this).parents("[name='prodPicker']").slideUp();
			return false;
		});
		
		//取消
		$("body").delegate("[name='prodCancel']", "click", function(){
			$(this).parents("[name='prodPicker']").slideUp("normal");
			return false;
		});
	};
};

$(function() {
	var prod = new Prod();
	prod.init();
});
